<template>
	<view class="content">
		<u-cell-group>
			<u-field
				v-model="name"
				label="账号"
				placeholder="请填写账号"
			>
			</u-field>
			<u-field
				v-model="pass"
				label="密码"
				placeholder="请填写密码"
			>
			</u-field>
		</u-cell-group>
		<div class='btn' >
			<u-button size="medium" :loading='loading' @click='login'>登录</u-button>
		</div>
		
		<!--提示-->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name:null,
				pass:null,
				loading:false,
			};
		},
		onLoad() {
			
		},
		methods:{
			login(){
				if(!this.name){
					this.$refs.uToast.show({
						title: '账号格式不正确',
						type: 'warning'
					})
				}else if(!this.pass){
					this.$refs.uToast.show({
						title: '密码格式不正确',
						type: 'warning'
					})
				}else{
					this.loading = true
					uni.request({
					    url: this.$api.appCarAppLogin, 
					    data: {
					        account:this.name,
							password:this.pass,
							//sign: '78d88a8cb24785856d461ec7e4b6955d',
							//timestamp: '1'
					    },
					    success: (res) => {
					        console.log(res);
							this.loading = false
							if(res.statusCode === 200){
								if(res.data.data){
									this.$refs.uToast.show({
										title: '登录成功',
										type: 'succes'
									})
									uni.setStorage({
									    key: 'userStatus',
									    data: res.data.data,
									    success: function () {
									        console.log('success');
											uni.switchTab({
											    url:'/pages/index/index'
											});
									    },fail() {
									    	console.log('error')
									    }
									});
								}else if(res.data.status === '500'){
									this.$refs.uToast.show({
										title: res.data.message,
										type: 'error'
									})
								}
							}
					    },
						fail:(err)=>{
							this.loading = false
					    	console.log(err)
							this.$refs.uToast.show({
								title: '与服务器连接失败，请检查网络状况',
								type: 'error'
							})
					    }
					});
				}
				
			}
		}
	}
</script>
<style lang="less">
	.btn{
		text-align: center;
		margin: 20rpx auto;
	}
</style>
